//
//  StageScrollerView.m
//  NewGame
//
//  Created by wwj on 15/9/24.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "StageScrollerView.h"
#import "StageRecordTool.h"
#import "FullBgView.h"
#import "StageView.h"
#import "StageModel.h"
#import "StageRecordModel.h"

@implementation StageScrollerView

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.contentSize = CGSizeMake(4 * self.frame.size.width, 0);

    self.pagingEnabled = YES;
    
    self.showsHorizontalScrollIndicator = NO;
    
    [self loadData];
    
    [self setBgImageView];
};

#pragma mark 设置背景图片
- (void)setBgImageView{
    [self addBgImageViewWithImageName:@"select_easy_bg.jpg" index:0];
    [self addBgImageViewWithImageName:@"select_normal_bg.jpg" index:1];
    [self addBgImageViewWithImageName:@"select_hard_bg.jpg" index:2];
    [self addBgImageViewWithImageName:@"select_insane_bg.jpg" index:3];
}

#pragma mark 添加图片
- (void)addBgImageViewWithImageName:(NSString *)imageName index:(int)index{
    FullBgView *bgView = [FullBgView fullBgView];
    bgView.imageName = imageName;
    CGSize size = bgView.frame.size;
    bgView.frame = CGRectMake(size.width * index, 0, size.width, size.height);
    [self addSubview:bgView];
    [self sendSubviewToBack:bgView];
}

#pragma mark 加载数据
- (void)loadData{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"stages.plist" withExtension:nil];
    NSArray *dataArray = [NSArray arrayWithContentsOfURL:url];
    
    CGFloat margeX = 30;//水平中间间距
    CGFloat margeY = 20;//垂直中间间距
    
    CGFloat stageViewW = [StageView stageView].frame.size.width;
    CGFloat stageViewH = [StageView stageView].frame.size.height;
    
    CGFloat X0 = (self.frame.size.width-(2*stageViewW)-margeX)*0.5+6;
    CGFloat Y0 = 150;
    
    for (int index = 0; index < dataArray.count; index++) {
        
        StageView *stageView =[StageView stageView];
        StageModel *stage = [StageModel stageWithDict:dataArray[index]];
        stage.no = index+1;
        //获取关卡数据
        stage.recordModel = [[StageRecordTool sharedStageRecordTool] stageRecordWithNo:stage.no];
        stageView.model = stage;
        stageView.tag = index + 1;
        CGFloat XM = self.frame.size.width*(index/6)+X0;
        CGFloat stageViewX = XM + (stageViewW +margeY)*(index/3%2);
        
        CGFloat stageViewY = Y0+(margeY+stageViewH)*(index%3);
        
        stageView.frame = CGRectMake(stageViewX, stageViewY, stageViewW, stageViewH);
        
        [self addSubview:stageView];
        [self sendSubviewToBack:stageView];
        
        //添加点击事件
        [stageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickStageView:)]];
    }
    
}

-(void)reloadDataWithNO:(int)number{
    //刷新当前关卡
    StageView *stage1 = (StageView *)[self viewWithTag:number];
    StageModel *model1 = stage1.model;
    model1.recordModel = [[StageRecordTool sharedStageRecordTool] stageRecordWithNo:number];
    stage1.model = model1;
    
    //刷新下一关
    StageView *stage2= (StageView *)[self viewWithTag:number + 1];
    if (stage2 ==nil) return;
    StageModel *model2 = stage2.model;
    model2.recordModel = [[StageRecordTool sharedStageRecordTool] stageRecordWithNo:number + 1];
    stage2.model = model2;
    
    //翻到下一页: 当前关卡通过 && 下一关从未玩过 && 下一关在下一页
    BOOL fail = ![@"f" isEqualToString:model1.recordModel.rank];
    BOOL never = model2.recordModel.rank == nil;
    BOOL nextPage = (number % 6 ==0);
    
    if (fail && never && nextPage) {
        CGPoint offset = self.contentOffset;
        offset.x = number / 6 * self.frame.size.width;
        [UIView animateWithDuration:0.3 animations:^{
            self.contentOffset = offset;
        }];
    }
    
}

-(void)onClickStageView:(UITapGestureRecognizer *)gestureRecognizer{
    if (_itemClickBlock) {
        StageView *stageView = (StageView *)gestureRecognizer.view;
        _itemClickBlock(stageView.model);
    }
}

@end
